Re: [GENERAL] Geometric, getting x and y co-ordinates GOING MAD!!!!! - Mailing list pgsql-general

From Stuart Rison
Subject Re: [GENERAL] Geometric, getting x and y co-ordinates GOING MAD!!!!!
Date
Msg-id v04020a04b37d8a4743a1@[128.40.242.190]
Whole thread Raw
In response to [GENERAL] Geometric, getting x and y co-ordinates from point datatype  (Stuart Rison <stuart@ludwig.ucl.ac.uk>)
Responses Re: [GENERAL] Geometric, getting x and y co-ordinates GOING MAD!!!!!  (selkovjr@mcs.anl.gov)
List pgsql-general
Following my original posting, I did a bit of hunitng in the source and
used them to come up with these functions:

/* get_x.c
   a function to return the x coord of a point */

#include "postgres.h"
#include "utils/geo_decls.h"

double
get_x(Point *pt)
{
    if (!PointerIsValid(pt))
        return NULL;

    return pt->x;
}    /* get_x() */


/* get_y.c
   a function to return the y coord of a point */

#include "postgres.h"
#include "utils/geo_decls.h"

double
get_y(Point *pt)
{
    if (!PointerIsValid(pt))
        return NULL;

    return pt->y;
}    /* get_y() */

... then I do ...

cc -c get_<x|y>.c -I/usr/local/pgsql/include
ld -G -Bdynamic -o get_<x|y>.so get_<x|y>.o

which gives me get_x.so and get_y.so

... then I do (within psql)...

brecard10=> CREATE FUNCTION get_<x|y>(point) RETURNS float
brecard10-> AS '/usr/people/postgres/get_<x|y>.so' LANGUAGE 'c';

AND NOW FOR THE CRAZY PART...

brecard10=> select * from points;
id|pos
--+-----------
 1|(1,2)
 2|(-1.3,4.77)
 3|(0,-3)
(3 rows)

brecard10=> select id,pos,get_x(pos) from points;
id|pos        |get_x
--+-----------+-----
 1|(1,2)      |    1
 2|(-1.3,4.77)| -1.3
 3|(0,-3)     |    0
(3 rows)

Rejoice I thought, I'm a genius, wealth, power and good looks will soon
follow...

brecard10=> select id,pos,get_y(pos) from points;
id|pos        |get_y
--+-----------+-----
 1|(1,2)      |    1
 2|(-1.3,4.77)| -1.3
 3|(0,-3)     |    0
(3 rows)

There goes the nobel, my seat in the parliament and my winning smile!

I have no training in C so I've reached this far by trial and error.  I
have however discovered that if write a function such as

/* pants.c
   weird of what! */
#include "postgres.h"
#include "utils/geo_decls.h"
double
pants(Point *pt)
{
    return 2.0;
}

and compile it as above and make it a function I get:

brecard10=> select id,pos,pants(pos) from points;
id|pos        |pants
--+-----------+-----
 1|(1,2)      |    1
 2|(-1.3,4.77)| -1.3
 3|(0,-3)     |    0
(3 rows)

HELP!
WHAT'S GOING ON!!!
Why can't I get to pt->y?
Why does function pants behave just like get_x (and get_y)?

Stuart.
+-------------------------+--------------------------------------+
| Stuart Rison            | Ludwig Institute for Cancer Research |
+-------------------------+ 91 Riding House Street               |
| Tel. (0171) 878 4041    | London, W1P 8BT, UNITED KINGDOM.     |
| Fax. (0171) 878 4040    | stuart@ludwig.ucl.ac.uk              |
+-------------------------+--------------------------------------+

pgsql-general by date:

Previous
From: James Thompson
Date:
Subject: Re: [GENERAL] Embedded SQL in 'C' (cursors)
Next
From: selkovjr@mcs.anl.gov
Date:
Subject: Re: [GENERAL] Geometric, getting x and y co-ordinates GOING MAD!!!!!